﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wfaFoxMigrator
{
    public partial class frmPreviewData : Form
    {   
        public frmPreviewData()
        {
            InitializeComponent();            
        }
        public void getTableData(ListItem TableItem, SortedList<string, DataTable> lstSchemas, SortedList<string, DataTable> lstData)
        {
            string strMsg = string.Empty;
            string strSql = string.Empty;
            string strSqlOriginal = string.Empty;
            DataTable dt = new DataTable();
            using (DataFunctions dfcs = new DataFunctions())
            {
                dt = dfcs.PrepareToMigrateThis(TableItem, ref lstSchemas, ref lstData, out strMsg);                
            }
            if (dt == null)
            {
                dt = new DataTable();
                dt.TableName = TableItem.Text;
                strSql = CommonFunctions.AnalizeColumnsAndTranslateSQL(dt);
                if (dt.Columns.Count > 0)
                {
                    DataTable ftDt = new DataTable();
                    string constr = String.Format(Properties.Settings.Default.FoxProCS, TableItem.FullPath);
                    using (DataFunctions dfcs = new DataFunctions { ConnectionString = constr })
                    {
                        strSqlOriginal = strSql;
                        ftDt = dfcs.ExecuteQueryOleDb(ref strSql);
                        if (ftDt == null && CommonFunctions.CheckIfItsJetEnabled())
                        {
                            dfcs.ConnectionString = Properties.Settings.Default.JetCS.Replace("{0}", TableItem.FullPath.ToLower().Replace(TableItem.TableName.ToLower(), string.Empty));
                            strSqlOriginal += ".dbf";
                            ftDt = dfcs.ExecuteQueryODBC(ref strSqlOriginal);
                        }
                    }
                    if (ftDt != null)
                    {
                        if (ftDt.Rows.Count > 0)
                        {
                            dtgPreviewData.DataSource = ftDt;
                        }
                    }
                }
            }
            else
            {
                dtgPreviewData.DataSource = dt;
            }
        }
    }
}
